package com.tanhua.server.controller;

import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/users")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TanhuaService tanhuaService;

    /**
     * description: 查询用户信息 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:14 <br>
     * author: SangWu <br>
     *
     * @param userID
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping
    public ResponseEntity getUserInfoById(Long userID) {
        UserInfoVo userInfo = userService.getUserInfoById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * description: 更新用户信息 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:27 <br>
     * author: SangWu <br>
     *
     * @param userInfo
     * @return org.springframework.http.ResponseEntity
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userService.updateUserInfoById(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 更新用户头像 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:28 <br>
     * author: SangWu <br>
     *
     * @param headPhoto
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/header")
    public ResponseEntity updateHead(MultipartFile headPhoto) throws IOException {
        userService.uploadHead(headPhoto);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/fans/{uid}")
    public ResponseEntity loveUser(@PathVariable Long uid) {
        tanhuaService.loveUser(uid);
        return ResponseEntity.ok(null);
    }
}
